package com.cbarmstrong.FinanceWeb;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cbarmstrong.Finance.Data;
import com.cbarmstrong.Finance.Ticker;
import com.cbarmstrong.Finance.TradeSet;
import com.cbarmstrong.MarketWatcher.IndicatorProcessor;

/**
 * Servlet implementation class profitGraph
 */
@WebServlet("/profitGraph")
public class profitGraph extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public profitGraph() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		OutputStream out = response.getOutputStream();
		IndicatorProcessor cop = new IndicatorProcessor();
		if(request.getParameterMap().containsKey("ind")){
			if(session.getAttribute("ticker") != null && session.getAttribute("stockid") != null && ((String) session.getAttribute("stockid")).compareTo(request.getParameter("id"))==0 ){
				cop.initiate((Ticker) session.getAttribute("ticker"));
				Data.getLogger().fine("Restoring ticker from session for"+cop.getTicker().getName());
			} else{
				try {
					Connection c=Data.initConnection();
					Statement s=c.createStatement();
					ResultSet r=s.executeQuery("SELECT name FROM stock WHERE id="+request.getParameter("id"));
					while(r.next()){
						Data.getLogger().fine("Saving ticker to session for "+r.getString("name"));
						session.setAttribute("stockid", request.getParameter("id"));
						Ticker t=new Ticker(r.getString("name"), new Integer(System.getProperty("tickerDays","1000")));
						session.setAttribute("ticker", t);
						cop.initiate(t);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				} 
			}
			for(String ind : request.getParameter("ind").split(":")){
				String[] inds=ind.split(" ");
				if(ind.split(" ").length>0){
					if(inds[0].compareTo("cross")==0) cop.addCrossIndicator(inds[1], inds[2]);
					if(inds[0].compareTo("range")==0) cop.addRangeIndicator(inds[1], inds[2], inds[3], new Integer(inds[4]));
				}
			}
			TradeSet ts=cop.execute();
			response.setContentType("image/jpeg");
			ts.writePicture(1024, 768, out);
		}
		out.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
